package org.example.config;

import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ConfigLoader {
    private Properties properties;
    private Map<String, Object> yamlData;
    private Map<String, String> configMap;

    public ConfigLoader() {
        this.configMap = new HashMap<>();
        this.properties = new Properties();
        this.yamlData = null;
        loadPropertiesFromClasspath("config.properties"); // 默认的properties文件名
        loadPropertiesFromExternal("config.properties"); // 从同目录加载properties文件
        loadYamlFromClasspath("config.yml"); // 默认的YAML文件名
        loadYamlFromExternal("config.yml"); // 从同目录加载YAML文件
    }

    private void loadPropertiesFromClasspath(String filename) {
        try (InputStream input = getClass().getClassLoader().getResourceAsStream(filename)) {
            if (input != null) {
                properties.load(input);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        for (String key : properties.stringPropertyNames()) {
            configMap.put(key, properties.getProperty(key));
        }
    }

    private void loadPropertiesFromExternal(String filename) {
        File externalFile = new File(filename);
        if (externalFile.exists()) {
            try (InputStream input = new FileInputStream(externalFile)) {
                properties.load(input);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            for (String key : properties.stringPropertyNames()) {
                configMap.put(key, properties.getProperty(key));
            }
        }
    }

    private void loadYamlFromClasspath(String filename) {
        Yaml yaml = new Yaml();
        try (InputStream input = getClass().getClassLoader().getResourceAsStream(filename)) {
            if (input != null) {
                yamlData = yaml.load(input);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (yamlData != null) {
            for (Map.Entry<String, Object> entry : yamlData.entrySet()) {
                configMap.put(entry.getKey(), entry.getValue().toString());
            }
        }
    }

    private void loadYamlFromExternal(String filename) {
        File externalFile = new File(filename);
        if (externalFile.exists()) {
            Yaml yaml = new Yaml();
            try (InputStream input = new FileInputStream(externalFile)) {
                yamlData = yaml.load(input);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            if (yamlData != null) {
                for (Map.Entry<String, Object> entry : yamlData.entrySet()) {
                    configMap.put(entry.getKey(), entry.getValue().toString());
                }
            }
        }
    }

    public String getProperty(String key) {
        return properties.getProperty(key);
    }

    public Object getYamlValue(String key) {
        if (yamlData != null) {
            return yamlData.get(key);
        }
        return null;
    }

    public String getOldConfigValue(String key) {
        String value = null;
        if (yamlData != null && yamlData.containsKey(key)) {
            Object yamlValue = yamlData.get(key);
            if (yamlValue != null) {
                value = yamlValue.toString();
            }
        }
        if (properties.containsKey(key)) {
            value = properties.getProperty(key);
        }
        return value;
    }

    public String getConfigValue(String key) {
        return configMap.get(key);
    }


    public static ConfigLoader create() {
        return new ConfigLoader();
    }

    public static void main(String[] args) {
        ConfigLoader configLoader = new ConfigLoader();

        // 获取 properties 中的值
        String propValue = configLoader.getProperty("key");
        System.out.println("Property Value: " + propValue);

        // 获取 YAML 中的值
        Object yamlValue = configLoader.getYamlValue("key");
        System.out.println("YAML Value: " + yamlValue);

        // 获取配置值
        String value = configLoader.getConfigValue("key");
        System.out.println("Config Value: " + value);
    }
}
